package com.cxyd.modules.wcs.controller;

import com.cxyd.common.annotation.LogOperation;
import com.cxyd.common.page.PageData;
import com.cxyd.common.utils.Result;
import com.cxyd.common.validator.ValidatorUtils;
import com.cxyd.common.validator.group.AddGroup;
import com.cxyd.common.validator.group.DefaultGroup;
import com.cxyd.common.validator.group.UpdateGroup;
import com.cxyd.modules.wcs.dto.PlcItemDTO;
import com.cxyd.modules.wcs.service.PlcItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("wcs/plc-item")
public class PlcItemController {
    @Resource
    private PlcItemService plcItemService;

    @GetMapping("page")
    @RequiresPermissions("wcs:plc-item:page")
    public Result<PageData<PlcItemDTO>> page(@RequestParam Map<String, Object> params) {
        final PageData<PlcItemDTO> page = plcItemService.page(params);
        return new Result<PageData<PlcItemDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wcs:plc-item:info")
    public Result<PlcItemDTO> get(@PathVariable("id") Long id) {
        PlcItemDTO data = plcItemService.get(id);

        return new Result<PlcItemDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wcs:plc-item:save")
    public Result save(@RequestBody PlcItemDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        plcItemService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wcs:plc-item:update")
    public Result update(@RequestBody PlcItemDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

//        如果修改的字符包含PLC，则不能进行更新
        if (dto.getItemName().contains("PLC")){
            return new Result().error(500,"不能修改[PLC]相关信号点,请联系PLC相关人员!");
        }
        plcItemService.update(dto);

        return new Result();
    }
}
